package com.wxl.common.wiget

import android.content.Context
import android.text.TextUtils
import android.view.KeyEvent
import android.view.View
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.CenterPopupView
import com.wxl.common.R
import com.wxl.common.base.AbsActivity
import io.reactivex.functions.Action
import kotlinx.android.synthetic.main.pop_network_error.view.*

class TipPop : CenterPopupView {

    private lateinit var tips: String
    private  var  action0: Action?=null
    private lateinit var action1: Action
    private var sureText:String?=null

    constructor(context: Context) : super(context)

    override fun getImplLayoutId(): Int {
        return R.layout.pop_network_error
    }

    override fun onCreate() {
        super.onCreate()
        tipPopContent.text = tips
        tipPopSureBtn?.let {
            if(!TextUtils.isEmpty(sureText)) {
                tipPopSureBtn.text = sureText
            }
        }
        if(action0 == null){
            tipPopBackBtn.visibility = View.GONE
            tipPopBtnSpaceView.visibility = View.GONE
        }
        tipPopBackBtn.setOnClickListener {
            if(action0 != null) {
                dismiss()
                action0?.run()
            }
        }
        tipPopSureBtn.setOnClickListener {
            dismiss()
            action1.run()
        }
    }

    /**
     * 显示提示框
     */
    fun showTip(tips: String,sureText:String?, action0: Action?, action1: Action): TipPop {
        this.tips = tips
        this.action0 = action0
        this.action1 = action1
        this.sureText = sureText
        return this
    }


    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_F1 || keyCode == KeyEvent.KEYCODE_BACK) {
            tipPopBackBtn.performClick()
            return true
        } else if (keyCode == KeyEvent.KEYCODE_F4 || keyCode == KeyEvent.KEYCODE_ENTER) {
            tipPopSureBtn.performClick()
        }
        return true
    }


    companion object {
        fun show(context: Context, tips: String, action0: Action, action1: Action) {
            val mTipPop = TipPop(context).showTip(tips,null, action0, action1)
            XPopup.Builder(context)
                .dismissOnTouchOutside(false)
                .asCustom(mTipPop)
                .show()
            (context as AbsActivity).addPop(mTipPop)
        }

        fun showSure(context: Context, tips: String,  action1: Action) {
            val mTipPop = TipPop(context).showTip(tips,null, null, action1)
            XPopup.Builder(context)
                .dismissOnTouchOutside(false)
                .asCustom(mTipPop)
                .show()
            (context as AbsActivity).addPop(mTipPop)
        }

        fun showSure(context: Context, tips: String,sureText:String,action1: Action) {
            val mTipPop = TipPop(context).showTip(tips, sureText,null, action1)
            XPopup.Builder(context)
                .dismissOnTouchOutside(false)
                .asCustom(mTipPop)
                .show()
            (context as AbsActivity).addPop(mTipPop)
        }
    }
}